package ink.lovejinhu.GoldenCodeOfProgrammerInterview;

import ink.lovejinhu.common.ListNode;

import java.util.HashMap;

/**
 * @author jinhu
 * created 2021-08-10 19:14
 */
public class Interview_0201 {
    /**
     * 移除重复节点
     *
     * @param head
     * @return
     */
    public ListNode removeDuplicateNodes(ListNode head) {
        HashMap<Integer, ListNode> v0 = new HashMap<>(); //存node
        HashMap<Integer, ListNode> v1 = new HashMap<>();  //存value
        int i = 0;
        while (head != null) {
            if (v1.containsKey(head.val)) {
                head = head.next;
            } else {
                v0.put(i, head);
                i++;
                v1.put(head.val, head);
                head = head.next;
            }
        }
        ListNode new_head = v0.get(0);
        ListNode temp = new_head;
        for (int j = 1; j < v0.size(); j++) {
            temp.next = v0.get(j);
            temp = v0.get(j);
        }
        temp.next=null;
        return new_head;
    }


}
